using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using static WaterCylinder;
/*
Dialogue类专门处理对话中的文字显示系统
提供了文字的显示方式
*/

public enum DialogueState{
    Start,
    Display,
    Stop,
    End,
}

public class Dialogue : MonoBehaviour
{    
    /// <summary>
    /// 创建对话
    /// </summary>
    /// <param name="strs"></param>
    /// <returns></returns>
    public static Dialogue CreatDialogue(string[] strs, bool start = false){
        GameObject obj = Creat(GetPrefab("DefaultObject/Dialogue"));
        Dialogue dia = obj.GetComponent<Dialogue>();
        dia.allSentence.AddRange(strs);
        if(start)dia.StartDialogue();
        return dia;
    }
    //public TextMeshPro text;
    public Text text;//文本对象

    [SerializeField]
    private string displaySentence;
    /// <summary>
    /// 获取当前显示结果
    /// </summary>
    /// <returns></returns>
    public string GetSentence(){
        return displaySentence;
    }

    /// <summary>
    /// 启动时运行
    /// </summary>
    [Tooltip("Event")]
    public DialogueEvent OnStart;
    /// <summary>
    /// 句子结束时运行
    /// </summary>
    [Tooltip("Event")]
    public DialogueEvent OnEndSentence;
    /// <summary>
    /// 结束时运行
    /// </summary>
    [Tooltip("Event")]
    public DialogueEvent OnEnd;

    public delegate string Delegate(string str);

    /// <summary>
    /// 设置并覆盖当前字符
    /// </summary>
    public Delegate OnChar;
    /// <summary>
    /// 设置并覆盖当前句子
    /// </summary>
    public Delegate OnSentence;

    [SerializeField]
    private List<string> allSentence;
    [SerializeField]
    [Tooltip("Disable")]
    private string sentenceNow;
    [SerializeField]
    [Tooltip("Disable")]
    private string charNow;
    [SerializeField]
    [Tooltip("Disable")]
    private int sentencePointer;
    [SerializeField]
    [Tooltip("Disable")]
    private int charPointer;
    [SerializeField]
    [Tooltip("Disable")]
    private float time = 0.1f;

    [SerializeField]
    private bool skip;
    /// <summary>
    /// 是否可以控制跳过
    /// </summary>
    public bool canSkip;

    /// <summary>
    /// 跳过
    /// </summary>
    public void Skip(){
        if(canSkip)skip = true;
    }

    /// <summary>
    /// 是否自动显示
    /// </summary>
    public bool isAuto;
    
    [SerializeField]
    private DialogueState state = DialogueState.Start;

    /// <summary>
    /// 初始化Dialogue
    /// </summary>
    public void Init(){
        sentencePointer = 0;
        charPointer = 0;
        if(allSentence.Count > 0)sentenceNow = allSentence[0];
        charNow = "";
        displaySentence = "";
        state = DialogueState.Start;
    }

    void Awake(){
        Init();
    }
    void Start(){
        StartCoroutine(process());
    }

    /// <summary>
    /// 开始显示
    /// </summary>
    public void StartDialogue(){
        Init();
        state = DialogueState.Display;
        OnStart?.Invoke(gameObject.name);
    }
    /// <summary>
    /// 开始显示
    /// </summary>
    /// <param name="list">显示的文本</param>
    public void StartDialogue(List<string> list){
        allSentence = list;
        StartDialogue();
    }
    /// <summary>
    /// 开始显示
    /// </summary>
    /// <param name="list">显示的文本（每一句使用分号";"隔开）</param>
    public void StartDialogue(string list){
        allSentence = new List<string>();
        allSentence.AddRange(list.Split(";"));
        StartDialogue();
    }
    
    //更改句子
    public string GetNewSentence(string s){
        return OnSentence!=null?OnSentence.Invoke(s):s;
    }

    //句子结束，切换到下一句
    void EndSentence(){
        charPointer = 0;
        sentencePointer++;
        if(sentencePointer >= allSentence.Count){
            OnEnd?.Invoke(gameObject.name);
            state = DialogueState.End;
        }else{
            sentenceNow = GetNewSentence(allSentence[sentencePointer]);
            displaySentence = "";
            state = DialogueState.Display;
        }
        OnEndSentence?.Invoke(gameObject.name);
    }

    string GetNewChar(string c){
        return OnChar!=null?OnChar.Invoke(c):c;
    }
    public string GetNewChar(char c){
        string s = c + "";
        return GetNewChar(s);
    }
    void MainProcess(){
        if(state == DialogueState.Display){
            if(skip){
                displaySentence = sentenceNow;
                sentencePointer = allSentence.Count;
                state = DialogueState.Stop;
                skip = false;
            }else{
                charNow = GetNewChar(sentenceNow[charPointer]);
                displaySentence += charNow;
                charPointer++;
                if(charPointer >= sentenceNow.Length){
                    //自动模式自动跳转到下一句
                    if(isAuto){
                        EndSentence();
                    }else{//非自动模式进入暂停状态，等待skip操作输入
                        state = DialogueState.Stop;
                    }
                }
            }
        }else if(state == DialogueState.Stop){
            if(skip){
                EndSentence();
                skip = false;
            }
        }
    }

    //文本提交到Text对象以显示
    void ToText(){
        if(text != null){
            text.text = displaySentence;
        }
    }

    protected IEnumerator process(){
        while(true){
            MainProcess();
            ToText();
            yield return new WaitForSeconds(time);
        }
    }
    
}
